$(function(){

    function allCategoty(){

        $.ajax({
            url:"/easybuyProductCategory/getAllProductCategory",
            success:function (data) {

                //商品分类详情
                var categoryList=data.productCategoryList;
                //楼层商品
                var floorProductList=data.floorProductList;

                $.each(categoryList,function (index,list1) {
                    $("#firstCategory").append('<li><a href="#'+list1.id+'">'+list1.name+'</a></li>');
                });

                //顶部全部商品分类
                $('#leftNav ul').html('');
                $.each(categoryList,function (index1,list1) {
                    var li=$('<li id="li'+index1+'"></li>');
                    //一级分类
                    var fj=$('<div class="fj">' +
                        '<span class="n_img"><span></span><img src="images/nav'+(index1+1)+'.png"/></span>' +
                        // '<span class="fl">'+category.name+'</span>' +
                        //'<span class="fl">' + list1.name + '</span>' +
                         '<span class="fl"><a href="CategoryList.html?categorylevel1id='+list1.id+'" style="color: navajowhite">'+list1.name+'</a></span>' +
                        '</div>');
                    var zj = $('<div class="zj" id="zj' + index1 + '" name="' + index1 + '"></div>');
                    // var t = -40 * index1;
                    // $.css('top', t + 'px');
                    var zj_l = $('<div class="zj_l"></div>');
                    $.each(list1.productCategoryList, function (index2, list2) {
                        //二级分类
                        var zj_l_c = $('<div class="zj_l_c" id="' + list2.id + '"></div>');
                        var h2 = $('<a href="CategoryList.html?categorylevel2id=' + list2.id + '"><h2>' + list2.name + '/</h2></a>');
                        zj_l_c.append(h2);
                        zj_l.append(zj_l_c);
                        //三级分类
                        $.each(list2.productCategoryList, function (index3, list3) {
                            var a = $('<a href="CategoryList.html?categorylevel3id=' + list3.id + '">' + list3.name + '</a>|');
                            zj_l_c.append(a);
                             var a2 = $('<a href="CategoryList.html?categorylevel3id=' + list3.id + '">' + list3.name + '</a>');
                             $('.fresh_txt_c:eq(' + index1 + ')').append(a2);
                        });
                        zj.append(zj_l);
                    });
                    zj.append(
                        '<div class="zj_r">' +
                        '<a href="#"><img src="images/n_img1.jpg" width="236" height="200"/></a>' +
                        '<a href="#"><img src="images/n_img2.jpg" width="236" height="200"/></a>' +
                        '</div>');
                    li.append(fj);
                    li.append(zj);
                    $('#leftNav ul').append(li);
                });

                //鼠标移入移出效果
                $(".leftNav ul li").hover(
                    function () {
                        $(this).find(".fj").addClass("nuw");
                        $(this).find(".zj").show();
                    }
                    ,
                    function () {
                        $(this).find(".fj").removeClass("nuw");
                        $(this).find(".zj").hide();
                    }
                );

                //商品楼层
                $.each(categoryList,function (index,list1) {
                    //楼层显示
                    var div = $('<div class="i_t mar_10" id="floor_'+(index+1)+'"></div>');
                    var floor = $(
                        '<div class="i_t mar_10">' +
                        '<span class="floor_num" id="'+list1.id+'">'+(index+1)+'F</span>' +
                        '<span class="fl">'+list1.name+'</span>' +
                        '</div>');
                    var span = $('<span class="i_mores fr"></span>');
                    $.each(list1.productCategoryList,function(index2,list2){
                        var a = $('<a href="CategoryList.html?categorylevel2id='+list2.id+'" target="_blank">&nbsp; &nbsp; &nbsp;'+list2.name+'</a> ');
                        span.append(a);
                    });
                    floor.append(span);
                    div.append(floor);
                    $('#longImg').next().append(div);

                    //楼层商品显示
                    var div2 = $('<div class="content"></div>');
                    var fresh_left = $(
                        '       <div class="fresh_left">' +
                        '            <div class="fre_ban">' +
                        '                <div id="imgPlay1">' +
                        '                    <ul class="imgs" id="actor1">' +
                        '                        <li><a href="#"><img src="images/fre_r.jpg" width="211" height="286"/></a></li>' +
                        '                        <li><a href="#"><img src="images/fre_r.jpg" width="211" height="286"/></a></li>' +
                        '                        <li><a href="#"><img src="images/fre_r.jpg" width="211" height="286"/></a></li>' +
                        '                    </ul>' +
                        '                    <div class="prevf">上一张</div>' +
                        '                    <div class="nextf">下一张</div>' +
                        '                </div>' +
                        '            </div>' +
                        '            <div class="fresh_txt">' +
                        '                <div class="fresh_txt_c"></div>' +
                        '            </div>' +
                        '        </div>');

                    var fresh_mid = $('<div class="fresh_mid"></div>');
                    var fresh_mid_ul = $('<ul></ul>');

                    $.each(floorProductList[index],function(index2,product){
                        var li = $(
                            '<li>' +
                            '    <div class="name"><a href="#">'+product.name+'</a></div>' +
                            '    <div class="price">' +
                            '        <font>￥<span>'+product.price+'</span></font> &nbsp; '+product.stock+'R' +
                            '    </div>' +
                            '    <div class="img"><a href="Product.html?id='+product.id+'" target="_blank"><img src="/files/'+product.fileName+'" width="185" height="155"/></a></div>' +
                            '</li >');
                        fresh_mid_ul.append(li);
                    });
                    fresh_mid.append(fresh_mid_ul);
                    var fresh_right = $(
                        '        <div class="fresh_right">' +
                        '            <ul>' +
                        '                <li><a href="#"><img src="images/fre_b1.jpg" width="260" height="220"/></a></li>' +
                        '                <li><a href="#"><img src="images/fre_b2.jpg" width="260" height="220"/></a></li>' +
                        '            </ul>' +
                        '        </div>');

                    div2.append(fresh_left);

                    div2.append(fresh_mid);
                    div2.append(fresh_right);
                    $('#longImg').next().append(div2);
                });

                $.each(categoryList,function(index,category){
                    $.each(category.productCategoryList,function(index1,categoryC){
                        var a=$(
                            '   <a href=CategoryList.html?categorylevel2id='+categoryC.id+' target="_blank">'+categoryC.name+'</a>'
                        );
                        $('.fresh_txt_c:eq('+index+')').append(a);
                    });
                });

            }
        });
    }

    allCategoty();

    $('#allCategory').on("hover",".zj",function () {
        $(this).css("display","block");
    },function () {
        $(this).css("display","none");
    });

    //获取最新的5条资讯
    $.ajax({
        url:"/easybuyNews/newsLatest",
        success:function (data) {
            var news=JSON.parse(data);

            for(var i=0;i<news.length;i++){
                $("#news").append('<li><a href="member.html" class="newsLatest" data-nid="'+news[i].id+'" >'+news[i].title+'</a></li>');
            }
        }
    });

    //跳转到对应资讯查看资讯详情
    $('#news').on("click",".newsLatest",function(){
        var id=$(this).data('nid');
        localStorage.setItem("nid",id);
    })

    //跳转到全部资讯页
    $('#more').click(function () {
        var id=$(this).data('nid');
        localStorage.setItem("nid",id);
    })

    //点击搜索按钮
    $('.s_btn').click(function(){
        var name = $(this).prev().val();
        if(name == ''){
            // alert("亲，要输入商品名称哦~~");
            // alert('亲，要输入商品名称哦~~');
            window.location.href='CategoryList.html?name='+'*'+'';
            $(this).prev().val('');
        }else{
            window.location.href='CategoryList.html?name='+name+'';
        }
    });

    //首页购物车
    var yonghu=JSON.parse(localStorage.getItem('obj'));
    if(yonghu!=null){
        var id=yonghu.user1.id;
        $.ajax({
            url:"/buyCar/getCarProductInPerson",
            data:{
                "userId":id
            },
            success:function (rs) {
                if(rs!=null&&rs!=""){
                    var productList=rs.productList;
                    var stockMap=rs.stockMap;
                    //购物车填入商品
                    fillBuyCar(productList,stockMap);
                }else{
                    $('.i_car>.car_t').html('购物车 [ <span>0</span> ]');
                    $(".un_login").html("购物车为空，快去添加商品吧！");
                    $(".price_a").remove();
                    $(".price_sum").remove();
                }
            }
        });
    }else{
        //未登录
        $('.i_car>.car_t').html('购物车 [ <span>0</span> ]');
        $('.un_login').html('还未登录！<a href="Login.html" style="color:#ff4e00;">马上登录</a> 查看购物车！');
        $(".price_a").remove();
        $(".price_sum").remove();
    }

    //判断登录是否超时
    var obj1=JSON.parse(localStorage.getItem('obj'));
    $.ajax({
        url:"/easybuyUser/getExpire",
        headers:{"token":obj1.token},
        success:function(data){
            if(data=="true"){
                localStorage.removeItem("obj");
                $('#login').css("display","inline-block");
            }else{
                var username=obj1.user1.userName;
                $('#username').html(username);
                $('#login').css("display","none");
                $('#logOut').css("display","inline-block");
                $('#adminM').css("display","inline-block");
            }
        }
    })


})